/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Util;

import Model.Usuario;
import org.hibernate.cfg.AnnotationConfiguration;
import org.hibernate.SessionFactory;
/**
 *
 * @author fagner
 */
public class HibernateUtil {

    private static SessionFactory sessionFactory;

    private HibernateUtil() {
        
    }

    public static SessionFactory getSessionFactory(Object objType) {

        if (sessionFactory == null) {
            try {
                // Create the SessionFactory from standard (hibernate.cfg.xml)
                // config file.
                AnnotationConfiguration ac = new AnnotationConfiguration();
                ac.addAnnotatedClass(objType.getClass());
                ac.configure();
                sessionFactory = ac.buildSessionFactory();
                
            } catch (Throwable ex) {
                // Log the exception.
                System.err.println("Initial SessionFactory creation failed." + ex);
                System.err.println("Mensagem" + ex.getMessage());
                throw new ExceptionInInitializerError(ex);
            }

            return sessionFactory;

        } else {
            return sessionFactory;
        }
        
    }
    
    public static void main(String[] args) {
        HibernateUtil.getSessionFactory(new Usuario());
    }
    

}
